package apy.codecase.greendao;

import android.content.Context;
import android.util.Log;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import apy.codecase.greendao.gen.AppDaoModelDao;
import apy.codecase.model.AppDaoModel;

/**
 * Created by Administrator on 2017/10/21.
 */

public class AppDaoUtils {
    private static final String TAG = AppDaoUtils.class.getSimpleName();
    private AppDaoManager mManager;

    public AppDaoUtils(Context context){
        mManager = AppDaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 完成AppDaoModel记录的插入，如果表未创建，先创建AppDaoModel表
     * @param AppDaoModel
     * @return
     */
    public boolean insertAppDaoModel(AppDaoModel AppDaoModel){
        boolean flag = false;
        flag = mManager.getDaoSession().getAppDaoModelDao().insert(AppDaoModel) == -1 ? false : true;
        Log.i(TAG, "insert AppDaoModel :" + flag + "-->" + AppDaoModel.toString());
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     * @param AppDaoModelList
     * @return
     */
    public boolean insertMultAppDaoModel(final List<AppDaoModel> AppDaoModelList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (AppDaoModel AppDaoModel : AppDaoModelList) {
                        mManager.getDaoSession().insertOrReplace(AppDaoModel);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     * @param AppDaoModel
     * @return
     */
    public boolean updateAppDaoModel(AppDaoModel AppDaoModel){
        boolean flag = false;
        try {
            mManager.getDaoSession().update(AppDaoModel);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     * @param AppDaoModel
     * @return
     */
    public boolean deleteAppDaoModel(AppDaoModel AppDaoModel){
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(AppDaoModel);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     * @return
     */
    public boolean deleteAll(){
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(AppDaoModel.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<AppDaoModel> queryAllAppDaoModel(){
        return mManager.getDaoSession().loadAll(AppDaoModel.class);
    }

    /**
     * 根据主键id查询记录
     * @param key
     * @return
     */
    public AppDaoModel queryAppDaoModelById(long key){
        return mManager.getDaoSession().load(AppDaoModel.class, key);
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<AppDaoModel> queryAppDaoModelByNativeSql(String sql, String[] conditions){
        return mManager.getDaoSession().queryRaw(AppDaoModel.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     * @return
     */
    public List<AppDaoModel> queryAppDaoModelByQueryBuilder(long id){
        QueryBuilder<AppDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(AppDaoModel.class);
        return queryBuilder.where(AppDaoModelDao.Properties._id.eq(id)).list();
    }
    public List<AppDaoModel> queryAppDaoModelByQueryBuilder(String id){
        QueryBuilder<AppDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(AppDaoModel.class);
        return queryBuilder.where(AppDaoModelDao.Properties._id.eq(id)).list();
    }

    public List<AppDaoModel> queryAppDaoModelByQueryBuilder1(String psw){
        QueryBuilder<AppDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(AppDaoModel.class);
        return queryBuilder.where(AppDaoModelDao.Properties.AppPsw.eq(psw)).list();
    }

    public List<AppDaoModel> queryAppDaoModelByQueryBuilder2(String psw){
        QueryBuilder<AppDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(AppDaoModel.class);
        return queryBuilder.where(AppDaoModelDao.Properties.Account.eq(psw)).list();
    }
}
